<template>
	<view class="zone">
		<view class="status_bar"></view>
		<Header title="实名认证" color="#fff"></Header>

		<view class="main">
			<view class="box">

				<view class="in">
					<view class="item">
						<input type="text" v-model="cert_name" :disabled="disabled" placeholder="请输入真实姓名">
					</view>
					<view class="item">
						<input type="text" v-model="cert_no" maxlength="20" :disabled="disabled" placeholder="请输入身份证号">
					</view>
					<view class="notice">
						<p>温馨提示：请输入正确的姓名和身份证号</p>
						<p>跳转支付宝人脸识别完成认证!</p>
					</view>
					<view v-if="userinfo.cert_no" class="button">人脸识别成功</view>
					<view v-else class="button" @click="ali_initialize()">跳转支付宝人脸识别</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#FEF3EC',
				},
				cert_name: '',
				cert_no: '',
				link: '',
				userinfo: '',
				disabled: false,
			};
		},
		onShow() {
			let certify_id = uni.getStorageSync('certify_id')
			this.ali_query(certify_id);
			this.getUserInfo();
		},
		methods: {
			// 请求用户信息接口
			async getUserInfo() {
				let res = await this.$http.index.getUserInfo()
				this.userinfo = res.data
				if (!!res.data.cert_no) {
					this.cert_name = res.data.cert_name
					this.cert_no = res.data.cert_no
					this.disabled = true
				}
			},

			// 支付宝身份认证--初始化获取certify_id接口
			async ali_initialize() {
				const reg = /<script[^>]*>([\S\s]*?)<\/script>/gim;
				const cert =  /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/

				// console.log(this.getage(this.cert_no)); 
				if (this.cert_name == '' || this.cert_name == null) {
					this.$u.toast('请输入真实姓名');
				} else if (this.cert_no == '' || this.cert_no == null) {
					this.$u.toast('请输入正确的身份证号码');
				} else if(!cert.test(this.cert_no)){
					this.$u.toast('身份证号码有误')
				}else if (this.getage(this.cert_no) < 16) {
					this.$u.toast('未成年人禁止认证!');
				} else if (reg.test(this.cert_no)) {
					this.$u.toast('存在特殊字符，请重新输入')
				} else {
					let res = await this.$http.index.ali_initialize({
						cert_name: this.cert_name,
						cert_no: this.cert_no,
						token: uni.getStorageSync('token')
					})
					if (res.code == 1) {
						console.log(res.certify_id);
						uni.setStorageSync('certify_id', res.certify_id)
						this.ali_auth(res.certify_id);
					} else {
						this.$u.toast(res.msg)
					}
				}
			},
			//支付宝身份认证开始认证接口
			async ali_auth(certify_id) {
				let res = await this.$http.index.ali_auth({
					certify_id: certify_id
				})
				// console.log(res);
				if(res.msg) {
					this.$u.toast(res.msg)
				}else {
					let url = res
					const certifyUrl = 'alipays://platformapi/startapp?appId=20000067&url=' + encodeURIComponent(url);

					if (this.$u.os() == 'ios') {
						location.href = certifyUrl
					} else if (this.$u.os() == 'android') {
						plus.runtime.openURL(certifyUrl)
					} else {
						location.href = certifyUrl
					}
				}
			},

			//支付宝认证查询结果接口
			async ali_query(certify_id) {
				let res = await this.$http.index.ali_query({
					certify_id: certify_id
				})
				this.getUserInfo();
			},


			getage(userCard) {
				let myDate = new Date();
				let month = myDate.getMonth() + 1;
				let day = myDate.getDate();
				let age = myDate.getFullYear() - userCard.substring(6, 10);
				if (userCard.substring(10, 12) < month || userCard.substring(10, 12) == month && userCard.substring(12,
					14) <= day) {
					age++;
				}
				return age;
			},
		},
	}
</script>

<style lang="less">
	.zone {
		background: linear-gradient(180deg, #0477FD 0%, #7AFDCF 100%);
		min-height: 100vh;
	}

	.main {
		padding: 10px;
	}

	.box {
		background:  rgba(255,255,255,0.7);
		box-shadow: 0px 2 4px 0px rgba(0,0,0,0.1);
		border-radius: 13px;
		padding: 15px;
	}

	.notice {
		p {
			text-align: left;
			color: #696969;
			font-size: 12px;
			font-weight: normal;
		}
	}


	.in {
		.inp {
			color: #B5D0F7;
			font-size: 14px;
			font-weight: 600;
		}

		.item {
			padding: 20px 10px;
			margin-bottom: 10px;
			background: #fff;
			border-radius: 4px;

			input {
				font-size: 13px;
				color: #696969;
			}
		}

		.button {
			background: url('http://img.cpgm.cc/panda/static/public/btn3.png');
			background-size: 100% 100%;
			width: 80%;
			height: 60px;
			color: #B73800;
			font-size: 16px;
			font-weight: 600;
			margin: 0 auto;
			margin-top: 40px;
		}
	}


	.web {
		height: 30vh;
		position: absolute;
		top: 50vh;
	}
</style>